package service

import (
	"ruoyi-hello/app/domain"
	"ruoyi-hello/app/sys/dao"
	"ruoyi-hello/common"
	"ruoyi-hello/utils"
	"ruoyi-hello/utils/error"
)

func Loign(req *domain.LoginReq) (domain.UserLogin, error.BusstionError) {

	sysUser, err := dao.GetUserByLoginName(req.UserName)
	if err.Err != nil {
		return domain.UserLogin{}, error.BusstionError{Code: error.SelectException.Code, Msg: "用户名密码错误", Err: nil}
	}
	if sysUser.UserId > common.ZEAR {
		if common.USER_DISABLE == sysUser.Status {
			return domain.UserLogin{}, error.BusstionError{Code: error.UserStatusDisableException.Code, Msg: error.UserStatusDisableException.Msg, Err: nil}
		}
		res := utils.PassVerify(req.Password, sysUser.Password)
		if !res {
			return domain.UserLogin{}, error.BusstionError{Code: error.SelectException.Code, Msg: "用户名密码错误", Err: nil}
		}
		return domain.UserLogin{UserId: sysUser.UserId, UserName: sysUser.UserName, DeptId: sysUser.DeptId}, error.BusstionError{Code: error.Ok.Code, Err: nil}
	}
	return domain.UserLogin{}, error.BusstionError{Code: error.SelectException.Code, Msg: "用户名密码错误", Err: nil}
}
